package com.yx.backstage.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yx.backstage.converter.DictMessageConverter;
import com.yx.backstage.model.DictMessage;
import com.yx.backstage.model.param.DictMessageParam;
import com.yx.backstage.model.vo.DictMessageVo;
import com.yx.backstage.service.DictMessageService;
import com.yx.backstage.config.global.BizException;
import com.yx.backstage.config.global.CommonResult;
import com.yx.backstage.enums.IntBoolean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author YangXiong
 */
@Component
public class DictManager {
    @Resource
    private DictMessageService dictMessageService;

    public CommonResult<List<DictMessageVo>> listByType(String type) {
        if (StringUtils.isEmpty(type)) {
            return CommonResult.success(null);
        }
        LambdaQueryWrapper<DictMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictMessage::getType, type);
        queryWrapper.eq(DictMessage::getIsChild, IntBoolean.YES.getKey());
        List<DictMessage> list = dictMessageService.list(queryWrapper);
        List<DictMessageVo> result = DictMessageConverter.handlerDictResult(list);
        return CommonResult.success(result);
    }

    public CommonResult<List<DictMessageVo>> allType() {
        QueryWrapper<DictMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_child", IntBoolean.NO.getKey());
        List<DictMessage> list = dictMessageService.list(queryWrapper);
        List<DictMessageVo> result = DictMessageConverter.handlerDictResult(list);
        return CommonResult.success(result);
    }

    public Boolean disableDict(DictMessageParam param) {
        if (Objects.isNull(param.getEnable())) {
            throw new BizException("参数不能为空");
        }
        Long id = param.getId();
        DictMessage message = dictMessageService.getById(id);
        if (Objects.isNull(message)) {
            throw new BizException("非法操作，字典不存在");
        }
        UpdateWrapper<DictMessage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("enable", param.getEnable());
        return dictMessageService.update(updateWrapper);
    }

    public Boolean saveOrUpdate(DictMessageParam param) {
        if (Objects.isNull(param)) {
            throw new BizException("参数不能为空");
        }
        DictMessage dictMessage = getDictMessage(param);
        setValue(param, dictMessage);
        return dictMessageService.saveOrUpdate(dictMessage);
    }

    private void setValue(DictMessageParam param, DictMessage dictMessage) {
        if (Objects.isNull(param.getValue())) {
            AtomicInteger value = new AtomicInteger(1);
            List<DictMessageVo> listByType = listByType(param.getType()).getData();
            if (CollectionUtil.isEmpty(listByType)) {
                dictMessage.setIsChild(1);
                dictMessage.setValue(1);
            } else {
                listByType.forEach(type -> {
                    if (type.getValue() > value.get()) {
                        value.set(type.getValue() + 1);
                    }
                });
                dictMessage.setValue(value.get());
            }
        }
    }

    private DictMessage getDictMessage(DictMessageParam param) {
        DictMessage dictMessage = new DictMessage();
        dictMessage.setId(param.getId());
        dictMessage.setIsChild(param.getIsChild());
        dictMessage.setName(param.getName());
        dictMessage.setRemark(param.getRemark());
        dictMessage.setValue(param.getValue());
        dictMessage.setType(param.getType());
        dictMessage.setEnable(param.getEnable());
        dictMessage.setText(param.getText());
        dictMessage.setOperatorName(param.getOperatorName());
        dictMessage.setParentId(param.getParentId());
        return dictMessage;
    }

    public CommonResult<DictMessageVo> getParentByType(DictMessageParam param) {
        LambdaQueryWrapper<DictMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictMessage::getType, param.getType());
        queryWrapper.eq(DictMessage::getIsChild, IntBoolean.NO.getKey());
        DictMessage one = dictMessageService.getOne(queryWrapper);
        if(Objects.isNull(one)){
            throw new BizException("不存在此字典");
        }
        DictMessageVo dictMessageVo = DictMessageConverter.getDictMessageVo(one);
        return CommonResult.success(dictMessageVo);
    }

    public Boolean deleteDict(DictMessageParam param) {
        if(Objects.isNull(param)){
            throw new BizException("参数不能为空");
        }
        DictMessage message = dictMessageService.getById(param.getId());
        if(Objects.isNull(message)){
            throw new BizException("非法操作，数据不存在");
        }
        return dictMessageService.removeById(param.getId());
    }
}
